package com.zd.springcloud.provider.service.impl;

import com.zd.springcloud.domain.Good;
import com.zd.springcloud.provider.mapper.GoodMapper;
import com.zd.springcloud.provider.service.GoodService;
import io.seata.core.context.RootContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodServiceImpl implements GoodService {
    @Autowired(required = false)
    GoodMapper goodMapper;

    @Override
    public void reduceStock(int num, Long goodId) {

        System.out.println(RootContext.inGlobalTransaction());
        System.out.println(RootContext.getXID());
        System.out.println(RootContext.getBranchType());
        //减库存时判断商品的库存数是否满足我们的商品数
        Good good = goodMapper.findGoodById(goodId);
        if(good.getGoodStock() < num){
           throw  new RuntimeException("库存不足" + good.getGoodName());
        }

        goodMapper.reduceStock(num,goodId);

    }
}
